package cn.ycl.system.other.config.scheduler.sendmsgscheduler;

import cn.ycl.system.biz.shop.entity.KillInfo;
import cn.ycl.system.biz.shop.service.KillInfoService;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author YuanChangLiang
 * @Date 2020/9/22
 * @Desc 最简单的定时发送短信的定时任务
 */
@Component
@EnableScheduling

public class SendMsgJob {
    @Resource
    private KillInfoService killInfoService;

    @Scheduled(cron = "0 0/30 * * * ?")
    public void process() {
        System.out.println("最简单的：发送短信ing.....");
    }


    /**
     * 定时任务：每天凌晨12:30修改秒杀时间
     */
    @Scheduled(cron = "0 30 0 * * ?")
    public void resetKillTime() throws ParseException {
        List<KillInfo> killInfos = killInfoService.queryAllByLimit(0, 3);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
        String format = sdf.format(date);
        String[] split = format.split("-");
        String day = split[0] + "-" + split[1] + "-" + split[2];
        if (killInfos.size() == 3) {
            KillInfo killInfo1 = killInfos.get(0);
            killInfo1.setKillStartTime(sdf.parse(day + "-08-30-00"));
            killInfo1.setKillEndTime(sdf.parse(day + "-14-00-00"));
            killInfoService.update(killInfo1);

            KillInfo killInfo2 = killInfos.get(1);
            killInfo2.setKillStartTime(sdf.parse(day + "-11-00-00"));
            killInfo2.setKillEndTime(sdf.parse(day + "-16-00-00"));
            killInfoService.update(killInfo2);

            KillInfo killInfo3 = killInfos.get(2);
            killInfo3.setKillStartTime(sdf.parse(day + "-15-30-00"));
            killInfo3.setKillEndTime(sdf.parse(day + "-23-00-00"));
            killInfoService.update(killInfo3);
            System.out.println("定时任务执行完毕");
        }
    }
}
